package com.itlaobing.response.body;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

//显示 图片
@WebServlet("/show/image")
public class ShowImageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setHeader("content-type","image/png ;charset=utf-8");

        String pathname = "E:/ayao.png";
        InputStream in = new FileInputStream(pathname);

        OutputStream out = response.getOutputStream();

        // 把 字节输入流 对接 到 字节输出流
        in.transferTo(out);// @since 9 ( Java 9 )
        // 等到当前的 service 方法执行完毕，
        // 容器(比如Jetty或Tomcat)就会将 响应对象 中的所有数据封装成 HTTP响应 随后将 HTTP响应 发送给客户端(浏览器)
    }
}
